from Tkinter import *

H='H'
V='V'

def colorToString(r,g=None,b=None):
	if g==None and b==None and len(r)==3:
		return "#%02x%02x%02x" % (r[0],r[1],r[2])
	else:
		return "#%02x%02x%02x" % (r,g,b)
def stringToColor(s):
	return (int(s[1:3],16),int(s[3:5],16),int(s[5:7],16))
		
def _stringToJustifyDict(): return {'left':LEFT, 'right':RIGHT, 'center':CENTER}
def stringToJustify(value): return _stringToJustifyDict()[value] 
def justifyToString(value): return dictInverse(_stringToJustifyDict())[value]

def _stringToReliefDict(): return {'sunken':SUNKEN, 'raised':RAISED,'groove':GROOVE,'ridge':RIDGE,'flat':FLAT}
def stringToRelief(value): return _stringToReliefDict()[value] 
def reliefToString(value): return dictInverse(_stringToReliefDict())[value]

def _stringToAnchorDict(): return {'center':CENTER,'n':N, 'ne':NE,'e':E, 'se':SE, 's':S, 'sw':SW, 'w':W, 'nw':NW}
def stringToAnchor(value): return _stringToAnchorDict()[value] 
def anchorToString(value): return dictInverse(_stringToAnchorDict())[value]


def dictInverse(d):
	dinv = {}
	for key,value in d.iteritems():
		dinv[value] = key
	return dinv
	
def stringToUnderlinePosition(s):
	# If given a string '&hello', underline the h.
	if '&' in s and '&&' not in s:
		pos = s.find('&')
		text = s.replace('&','',1)
		return text, pos
	else:
		return s , -1

def getbasecontrol(control):
	bHasTkControl = True
	try:
		control.basecontrol
	except:
		bHasTkControl = False
	if not bHasTkControl: return control
	else: return control.basecontrol
	
def underlinePositionToString(s, pos):
	if pos==-1: return s
		
	return s[0:pos] + '&' + s[pos:] 
